package src;

import java.io.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {

        Map<String,Integer> map=new HashMap<>();
        //3+1数据库
        //在数据库中查找记录，优先查找有未交作业记录的同学
        Connection connection = null;
        Statement statement = null;

        ResultSet resultSet = null;
        String sql = "select * from name WHERE COUNT != 0";
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");

            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",
                    "root",
                    "123456"

            );
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            statement.execute(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader=new BufferedReader(new FileReader("D:\\names.txt"));
            String sno=null;

            while ((sno=reader.readLine())!=null){
                map.put(sno,0);
            }
            File file=new File("D:\\作业");
            File[] files=file.listFiles();

            //遍历文件夹下每个文件
            for (File f:files){

                String fileName=f.getName();

                Matcher matcher = Pattern.compile("信本1班(\\d+)+(.*)")
                        .matcher(fileName);

                while (matcher.find()) {

                    for (Map.Entry<String,Integer> m:map.entrySet()){

                        String sno1=matcher.group(1);
                        if (sno1.equals(resultSet)){
                            map.put(sno1,map.get(sno1)+1);
                        }
                        if (sno1.equals(m.getKey())){

                            map.put(sno1,map.get(sno1)+1);

                        }

                    }

                }
            }
            for (Map.Entry<String,Integer> n:map.entrySet()){

                if (n.getValue()==0){

                    System.out.println(n.getKey()+"   "+n.getValue());
                }

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

        }


    }}